package com.hmall.user.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {

    private static ThreadLocal<String> userIdThreadLocal = new ThreadLocal<>();

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从请求头中获取用户身份信息
        String userId = request.getHeader("authorization");

        // 将用户身份信息保存到ThreadLocal中
        userIdThreadLocal.set(userId);
        return true;
    }
}
